﻿using System.Windows.Forms;
using System.Threading;
using System;

namespace DotNet.Common
{
    public partial class FormProcessBar : Form
    {
        MethodInvoker _processFunction;

        public string Tip
        {
            get { return this.Text; }
            set { this.Text = value; }
        }

        public FormProcessBar(MethodInvoker processFunction)
        {
            InitializeComponent();

            _processFunction = processFunction;
        }

        protected override void OnShown(System.EventArgs e)
        {
            base.OnShown(e);

            Thread runThread = new Thread(new ThreadStart(Run));
            runThread.Start();
        }

        private void Run()
        {
            if (_processFunction != null)
            {
                _processFunction();
            }
            this.Invoke((MethodInvoker)this.Close);
        }

        public static void Run(Form fatherForm, string tip, MethodInvoker processFunction)
        {
            FormProcessBar oForm = new FormProcessBar(processFunction);
            if (!string.IsNullOrEmpty(tip))
            {
                oForm.Tip = tip;
            }
            oForm.ShowDialog(fatherForm);
        }

        public static void Run(Form fatherForm, MethodInvoker processFunction)
        {
            FormProcessBar oForm = new FormProcessBar(processFunction);
            oForm.ShowDialog(fatherForm);
        }
    }
}
